<template>
  <div class="app-container warehouse-mange">
    <avue-form
      :option="queryOption"
      v-model="queryParams"
      ref="searchForm"
      v-show="showSearch"
      @keydown.enter="handleQuery"
    >
      <template #menu-form="{ size }">
        <el-button
          icon="Search"
          type="primary"
          :size="size"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" :size="size" @click="handleEmpty(searchForm)"
          >重置</el-button
        >
      </template>
    </avue-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['financial:warehouseManage:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="EditPen"
          plain
          @click="handleUpdate"
          :disabled="disUpdate"
          v-hasPermi="['financial:warehouseManage:edit']"
          >编辑</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Delete"
          plain
          @click="handleDelete"
          :disabled="disDelete"
          v-hasPermi="['financial:warehouseManage:dele']"
          >删除</el-button
        >
      </el-col>
      <!-- <right-toolbar
        @queryTable="getList"
        v-model:showSearch="showSearch"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      row-key="id"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column prop="number" label="仓库编号" width="200" />
      <el-table-column prop="name" label="仓库名称" width="230" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="managerName" label="库管员" width="200" />
      <el-table-column prop="unitName" label="归属公司" width="160" />
      <el-table-column prop="createName" label="创建人" width="100" />
      <el-table-column prop="createTime" label="创建日期" width="200">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.warehouseDate, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column prop="type" label="票据类型" width="200">
        <template #default="scope">
          <dict-drop :options="warehouse_manage_type" :value="scope.row.type" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        width="100"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="CircleCheckFilled"
            size="small"
            class="px-1"
            @click="handleInventory(scope.row)"
            >库存</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 人员弹出框下拉框 -->
    <SelectDialog
      :dialog-param="dialogParam"
      @ok="selectDialogData"
      @close="
        val => {
          dialogParam.isDialog = false
        }
      "
    ></SelectDialog>
  </div>
</template>
<script setup name="WarehouseMange">
import { reactive, toRefs, ref } from 'vue'
import { useRouter } from 'vue-router'
import {
  listWarehouse,
  delWarehouse,
  setWarehouse
} from '@/api/warehouseManage/warehouse'
import { listSalesOrderSelect } from '@/api/indentManage/indent'
import useCommonStore from '@/store/modules/common'
import {
  companyOption //归属公司
} from '@/hooks/dict/order'
import { getTreeNodeByParam, getPricipalId } from '@/utils/commo'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const commonStore = useCommonStore()
const props = defineProps({
  warehouseData: {
    type: Object,
    require: false,
    default: () => {
      return {}
    }
  }
})
const emit = defineEmits([])
const router = useRouter()
const { proxy } = getCurrentInstance()
const { warehouse_type, zero_exit_controller } = proxy.useDict(
  'warehouse_type', //仓库类型
  'zero_exit_controller' //零出库控制
)
const userOption = ref({ managerIdArr: [] })
const data = reactive({
  queryParams: {
    unit: userStore.userInfo.orgId,
    pageNum: 1,
    pageSize: 10
  },
  ids: [],
  selectArr: [],
  total: 0,
  queryOption: {
    labelWidth: '10',
    menuSpan: 5,
    submitBtn: false,
    // gutter: 30,
    emptyBtn: false,
    column: [
      {
        // label: '姓名',
        prop: 'number',
        span: 3,
        placeholder: '编号'
      },
      {
        prop: 'name',
        span: 3,
        placeholder: '名称'
      },
      {
        prop: 'address',
        span: 3,
        placeholder: '地址'
      },
      {
        // label: '姓名',
        prop: 'type',
        span: 3,
        placeholder: '仓库类型',
        type: 'select',
        dicData: warehouse_type
      },
      {
        prop: '_managerIdArr',
        span: 3,
        placeholder: '库管员',
        type: 'select',
        clearable: false,
        props: {
          value: 'uuid'
        },
        popperClass: 'className',
        dicData: computed(() => {
          return userOption.value['managerIdArr']
        }), //userOption.value['managerIdArr'],
        multiple: true,
        click: ({ value, column }) => {
          dialogParam.value.type = 'managerIdArr'
          dialogParam.value.title = '请选择发票经手人'
          dialogParam.value.isDialog = true
          dialogParam.value.multiple = true
          dialogParam.value.nodeKey = 'uuid'
          dialogParam.value.defaultCheckedKeys = queryParams.value._managerIdArr
          dialogParam.value.dataType = 'person'
        }
      },
      {
        prop: 'unit',
        span: 3,
        placeholder: '所属公司',
        type: 'tree',
        defaultExpandAll: true,
        disabled: userStore.userInfo.orgId != 484,
        props: {
          value: 'id', //   value: 'deptId',
          label: 'label' //   label: 'deptName'
        },
        dicData: companyOption
      }
      // {
      //
      //   prop: 'orderBelongDept',
      //   span: 3,
      //   placeholder: '订单所属部门',
      //   type: 'tree',
      //   props: {
      //     value: 'deptId',
      //     label: 'deptName'
      //   },
      //   dicData: []
      // },
    ]
  },
  showSearch: true,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: [],
  dialogParam: {} /* 人员弹窗框参数 */
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  queryOption,
  showSearch,
  disUpdate,
  disDelete,
  loading,
  tableList,
  dialogParam
} = toRefs(data)
// 提交
function handleQuery() {
  getList()
}
// 重置
function handleEmpty(formEl) {
  // proxy.$refs.searchForm.resetForm()
  proxy.resetForm('searchForm')
  getList()
}

// 初始化
async function init() {
  // 归属部门
  // let response = await commonStore.getDeptTreeData()
  // queryOption.value.column[3].dicData = proxy.handleTree(response, 'deptId')

  getList()
}
// 获取数据列表
function getList() {
  loading.value = true

  // 发票经手人
  queryParams.value.managerIdArr = getPricipalId(
    userOption.value['managerIdArr'],
    queryParams.value._managerIdArr
  )
  listWarehouse(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
    console.log(' tableList.value==>', tableList.value)
  })
  console.log('获取数据')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  disUpdate.value = selection.length != 1
  disDelete.value = !selection.length
}

function goTo(url, data) {
  router.push({ path: url, query: data })
}
// 新增
function handleAdd() {
  goTo('/source/sourceModules/warehouseForm')
}
// 编辑
function handleUpdate() {
  goTo('/source/sourceModules/editWarehouse', { warehouseId: ids.value[0] })
}
// 库存
function handleInventory(row) {
  goTo('/source/sourceModules/inventoryList', {
    warehouseId: row.id,
    warehouseName: row.name
  })
}
// 删除
function handleDelete(row) {
  let rowIds = row.id || ids.value
  console.log('selectArr-->', selectArr.value, 'id==>', rowIds)
  proxy.$modal
    .confirm('是否删除该仓库')
    .then(function () {
      return delWarehouse(rowIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/* 人员确认 */
const selectDialogData = val => {
  switch (val.type) {
    case 'managerIdArr':
      userOption.value.managerIdArr = val.selectData
      // queryOption.value.column[2].dicData = val.selectData
      queryParams.value._managerIdArr = val.defaultCheckedKeys
      break
  }
}

init()
</script>
<style scoped></style>
